package main

import (
	"bufio"
	"fmt"
	"net"
	"net/http"
)

func main() {

	var token = "dswest"
	conn, err := net.Dial("tcp", "127.0.0.1:9002")
	if err != nil {
		fmt.Println(err)
		return
	}

	if _, err := conn.Write([]byte(token)); err != nil {
		fmt.Println(err)
		return
	}

	for {

		conn2, err := net.Dial("tcp", "14.215.177.38:80")
		if err != nil {
			fmt.Println(err)
			break
		}

		req, err := http.ReadRequest(bufio.NewReader(conn))
		if err != nil {
			fmt.Println("读取 request 失败！")
			break
		}

		req.Host = "www.baidu.com"

		if err := req.WriteProxy(conn2); err != nil {
			fmt.Println("转发 request 失败！")
			break
		}

		response, err := http.ReadResponse(bufio.NewReader(conn2), req)
		if err != nil {
			fmt.Println("读取响应内容失败！")
			break
		}

		if response.Write(conn) != nil {
			fmt.Println("响应转发失败！")
			break
		}
	}

	fmt.Println("服务器异常退出", err)

}
